﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagRepository.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains TagRepository class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;

namespace iBuproModel.Repositories.Classes
{
    using System;
    using System.Linq;
    using System.Text;

    using iBuproModel.Repositories.Interfaces;

    /// <summary>
    /// Implements ITagRepository interface using linq to entities.
    /// </summary>
    public class TagRepository : GenericRepository<Tag>, ITagRepository
    {
        public void SaveTagsForProject(string tags, Project project)
        {
            this.SaveTagsForProject(this.ParseTags(tags), project);
        }

        public void SaveTagsForProject(string[] tags, Project project)
        {
            project.Tags.Clear();
            foreach (string tag in tags)
            {
                if (String.IsNullOrEmpty((tag.Trim()) ))
                    continue;

                Tag storedTag = context.Tags.FirstOrDefault(t => t.Name == tag);

                if (storedTag == null)
                {
                    project.Tags.Add(new Tag
                    {
                        Name = tag
                    });
                }
                else
                {
                    storedTag.Projects.Add(project);
                }

            }
        }

        public void UpdateTagsForProject(string tags, int id)
        {
            this.UpdateTagsForProject(this.ParseTags(tags), id);
        }


        public void UpdateTagsForProject(string[] tags, int id)
        {
            IProjectRepository projectRepository = repositoryService.ProjectRepositoryCreateInstance();
            Project stored = projectRepository.First(p => p.Id == id);

            this.UpdateTags(tags, stored);
        }

        private void UpdateTags(string[] tags, Project project)
        {
            project.Tags.Clear();

             foreach (string tag in tags)
            {
                Tag storedTag = context.Tags.FirstOrDefault(t => t.Name == tag);

                if (storedTag == null)
                {
                    project.Tags.Add(new Tag
                    {
                        Name = tag
                    });
                }
                else
                {
                    storedTag.Projects.Add(project);
                }
            }
        }

        private string[] ParseTags(string tags)
        {
            char[] separators = new char[] { ',', ';', '\t', };
            string[] field = tags.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < field.Length; i++)
            {
                field[i] = field[i].Trim();
            }

                return field;
        }


        public string GetTagStringForProject(Project project)
        {
            StringBuilder line = new StringBuilder();
            foreach (Tag tag in project.Tags.ToList())
            {
                line.Append(tag.Name);
                line.Append(" ");
            }

            return line.ToString();
        }


        public void RemoveTagsForProject(int id)
        {
            IDiplomProjectRepository diplomProjectRepository = repositoryService.DiplomProjectRepositoryCreateInstance();

            Project project = diplomProjectRepository.First(p => p.Id == id);

            project.Tags.Clear();
            context.Attach(project);
            context.Projects.ApplyCurrentValues(project);
        }

        public string TagsToString(IEnumerable<Tag> tags)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < tags.Count(); i++)
            {
                builder.Append(tags.ElementAt(i).Name);
                if (i < tags.Count() - 1)
                    builder.Append(", ");

            }
            return builder.ToString();
        }
    }
}
